import json

from flask import jsonify, make_response, request, g
from flask_jwt_extended import get_jwt_identity
from flask_restful import Resource
from sqlalchemy.orm import aliased

from apps.model.serializeModel import model_to_list, model_to_dict
from apps.settingApi.switchCase.casePrimaryKey import caseGetPrimaryKeyData
from apps.settingApi.switchCase.serializeParameter import serializeGetFilter, pageResultData, getArgsData, \
    serializeParData

from apps.settingApi.switchCase.settingCurd import getPk, deletePk, putPk
from apps.settingApi.api.printCode.model import printTemplateModel,printOrderModel
from apps.settingApi.utils import getDataTime
from comm.sqlalchemyConfig import db
from comm.decorator import login_required
from apps.settingApi.utils import getLessees
printTemplateViewModel = printTemplateModel
printOrderViewModel = printOrderModel

class printOrderListView(Resource):
    # 查询用户列表
    @login_required()
    def get(self):
        result = printOrderViewModel.getAll()
        objData = {}
        objData["data"] = result
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)


class printTemplateListView(Resource):
    # 查询用户列表
    @login_required()
    def get(self):
        # 获取所有的参数
        args = request.args
        argsData = {}
        argsData["lessees_id"] = args.get('lessees_id', None)
        argsData["name"] = args.get('name', None)
        argsData["print_order_code"] = args.get('print_order_code', None)
        argsData = getLessees(argsData)
        print(argsData)
        result = printTemplateViewModel.getAll(**argsData)
        objData = {}
        objData["data"] = result
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def post(self):
        # 获取参数
        args,err = getArgsData()
        if err!=None:
            return make_response(jsonify({'msg': err}), 202)
        id_z = caseGetPrimaryKeyData()
        args = getLessees(args)
        args['id'] = id_z
        # 序列化跟校验参数
        addData, err = serializeParData(model=printTemplateViewModel, argsData=args)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        addData['template'] = json.dumps(addData['template'],ensure_ascii = False)
        try:
            add = printTemplateViewModel(**addData)
            db.session.add(add)
            db.session.commit()
            db.session.flush()
            objData = model_to_dict(add, [])
        except Exception as e:
            return make_response(jsonify({'msg': '新增失败' + repr(e)}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)
class printTemplatePkView(Resource):
    pass
#
# class printCodePkView(Resource):
    @login_required()
    def get(self, pk):
        objData ,err = printTemplateViewModel.getPk(pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def put(self, pk):
        # 获取所有参数
        args, err = getArgsData()
        if err != None:
            return make_response(jsonify({'msg': err, }), 202)
        # 序列化跟校验参数
        addData, err = serializeParData(model=printTemplateViewModel, argsData=args)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        addData['template'] = json.dumps(addData['template'], ensure_ascii=False)
        print(addData)
        objData, err = putPk(printTemplateViewModel, pk, addData)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)

    @login_required()
    def delete(self, pk):
        objData, err = deletePk(printTemplateViewModel, pk)
        if err != None:
            return make_response(jsonify({'msg': err}), 202)
        return make_response(jsonify({'msg': '成功', 'result': objData}), 200)